"""
根据传入的参数，进行路径规划
"""
from flask import Blueprint, request, jsonify
import ast
import numpy as np
from server.utils.formatData import format_request_params

from werkzeug.datastructures import ImmutableMultiDict

from algorithm.pathPlanning.pathPlanning import PathPlanning


route_path_planning = Blueprint('path_planning', __name__)


# 规划路径
@route_path_planning.route('/get_path', methods=['POST'])
def get_path():
    if request.method == 'POST':
        # uid = request.form.get('uid')
        request.form = format_request_params(request.form)
        start_node = request.form.get('start_node')
        goal_node = request.form.get('goal_node')
        key_nodes = request.form.get('key_nodes')

        nodes_data = request.form.get('nodes_data')
        edges_data = request.form.get('edges_data')

        cost_v = np.array(request.form.get('cost_v'))
        cost = np.array(request.form.get('cost'))
        edges = np.array(ast.literal_eval(request.form.get('edges').replace('false', 'False').replace('true', 'True')))

        key_id = ['node-1', 'node-5', 'node-8', 'node-9']

        pathPlanning = PathPlanning(start_node, goal_node, key_nodes, key_id, cost_v, edges, cost)
        path = pathPlanning.get_path()

        return jsonify(path)
